import router from './router'
import store from './store'
// import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar 虚拟进度条
import 'nprogress/nprogress.css' // progress bar style 虚拟进度条样式
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'
import { getTokenTime } from '@/utils/auth'
import getPageTitle from '@/utils/get-page-title'

// const nowTime = Date.now()

const guoqitime = 60 * 60 * 2

function guoqi() {
  const nowTime = Date.now()
  // console.log(Date.now())
  // console.log(getTokenTime())
  return (nowTime - getTokenTime()) / 1000 > guoqitime
}
// console.log(guoqi())
const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  // console.log(to)
  // console.log(from)
  // console.log(next)
  document.title = getPageTitle(to.meta.title)
  if (store.state.user.key) {
    // 如果在登录页就直接进主页面
    if (guoqi()) {
      store.commit('user/delToken')
      store.commit('user/delUserInfo')
      router.push('/login')
      return Promise.reject(new Error('登录过期'))
    }
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      // 如果不再登录页可以放行进入下一个页面
      if (!store.state.user.userinfo.userId) {
        await store.dispatch('user/getUserInfo')
      }

      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      // console.log('有白名单放行')
      next()
    } else {
      // console.log('没有白名单请先登录')
      next('/login')
      NProgress.done()
    }
    // next()
  }
  // next()
})

router.afterEach(() => {
  NProgress.done()
})

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
